package com.demo.spring_demo.utils;

import com.demo.spring_demo.mapper.UserMapper;
import com.demo.spring_demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;


@Component
public class Utils {

    private static final String SENDEMAIL = "2066027342@qq.com";
    private static long account = 0;
    private static Random random = new Random();
    @Autowired
    private UserMapper userMapper;
    //简单邮件发送的类
    @Autowired
    private JavaMailSenderImpl mailSender;

    /**
     * 1、创建、发送cookie、以及销毁cookie
     *
     * @param user
     * @param request
     * @param response
     */
    public void sendCookie(User user, HttpServletRequest request, HttpServletResponse response) {
        //复选框是否选中
        if (user.isChecked()) {
            //创建cookie
            Cookie cookie_username = new Cookie("cookie_username", user.getUsername());
            Cookie cookie_password = new Cookie("cookie_password", user.getPassword());
            Cookie cookie_checked = new Cookie("cookie_checked", user.isChecked() + "");
            //持久化时间30分钟
            cookie_username.setMaxAge(60 * 30);
            cookie_password.setMaxAge(60 * 30);
            cookie_checked.setMaxAge(60 * 30);
            //创建cookie的路径
            cookie_username.setPath(response.getContentType());
            cookie_password.setPath(response.getContentType());
            cookie_checked.setPath(response.getContentType());
            //向客户端发送cookie
            response.addCookie(cookie_username);
            response.addCookie(cookie_password);
            response.addCookie(cookie_checked);
            System.out.println("cookie成功发送！");
        } else {
            //销毁cookie内容
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if ("cookie_username".equals(cookie.getName()) || "cookie_password".equals(cookie.getName()) || "cookie_checked".equals(cookie.getName())) {
                        cookie.setMaxAge(0);
                        response.addCookie(cookie);
                    }
                }
            }
        }
    }

    /**
     * 2、账号生成器
     */
    public String generateString() {
        int a = (int) (Math.floor(Math.random() * 10) + 1);
        account = account + a;
        return account + "";
    }

    @PostConstruct
    public void start() {
        //数据库最后一个人的账号
        account = Long.parseLong(userMapper.getUsername());
    }

    //验证码生成:
    public String getSms(String email, String title) {
        //1,随机生成四位验证码:
        String SmsCode = Integer.toString(1000 + random.nextInt(9000));
        //2、创建简单邮件发送对象
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //3、设置邮件标题
        simpleMailMessage.setSubject("register".equals(title)? "注册账户":"忘记密码");
        //4、发送验证码到对应邮箱
        simpleMailMessage.setText("验证码：" + SmsCode);
        //5、验证码的来源
        simpleMailMessage.setFrom(SENDEMAIL);
        //6、发送到指定邮箱
        simpleMailMessage.setTo(email);
        //7、发送
        mailSender.send(simpleMailMessage);
        //返回验证码
        return SmsCode;
    }

}
